package com.iamteer.day17;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import org.junit.Test;

import com.mysql.jdbc.Driver;

public class JDBCtest {
	private String url = "jdbc:mysql://localhost:3306/day17";
					//jdbc协议:数据库子协议:主机:端口/连接的数据库
	private String user = "root";	//用户名
	private String password = "123456";	//密码
	@Test
	public void test1() throws Exception {
		//1 创建驱动类对象
		Driver driver = new Driver();
		//Driver driver = new org.gjt.mm.mysql.Driver(); //旧版本
		
		//2 配置用户名和密码
		Properties properties = new Properties();
		properties.setProperty("user", user);
		properties.setProperty("password", password);
		
		//3 连接数据库，返回连接对象
		Connection conn = driver.connect(url, properties);
		System.out.println(conn);
	}
	
	public void test3() throws Exception{
		//Driver driver = new com.mysql.jdbc.Driver();
		
		//通过得到字节码对象的方式加载静态代码块，从而注册驱动程序
		Class.forName("com.mysql.jdbc.Driver");
		
		//Driver driver2 = new com.oracle.jdbc.Driver();
		//1.注册驱动程序(可以注册多个驱动程序)
		//DriverManager.registerDriver(driver);
		//DriverManager.registerDriver(driver2);
		
		//2.连接到具体的数据库
		Connection conn = DriverManager.getConnection(url, user, password);
		System.out.println(conn);
		
	}

	@Test
	public void test2() throws Exception {
		//通过得到字节码对象的方式加载静态代码，从而注册驱动程序
		Class.forName("com.mysql.jdbc.Driver");
		
		Connection conn = DriverManager.getConnection(url, user, password);
		System.out.println(conn);
	}
}
